from sqlalchemy import select, func
from sqlalchemy.ext.asyncio import AsyncSession

from server.config.session import with_session
from server.module_chat.business.flfj.entity.do.data_directory import DataDirectory
from server.module_chat.business.flfj.entity.do.data_resource import DataResource
from server.module_chat.business.flfj.entity.do.resource_info import ResourceInfo


class FlfjDao:
    @classmethod
    @with_session(session_key='secondary')
    async def build_graph(cls, session: AsyncSession):
        business_system_list = (
            await session.execute(
                select(DataDirectory.system_name)
                .where(
                    DataDirectory.system_name != '',
                    DataDirectory.system_name is not None)
                .distinct()
            )
        ).scalars().all()

        return business_system_list

    @classmethod
    @with_session(session_key='secondary')
    async def get_business_system_list(cls, session: AsyncSession):
        business_system_list = (
            await session.execute(
                select(
                    DataDirectory.system_name,
                    DataDirectory.data_owner,
                    DataDirectory.data_manager
                )
                .where(
                    DataDirectory.main_data_type == 1,
                    DataDirectory.system_name != '',
                    DataDirectory.system_name is not None)
                .distinct()
            )
        ).mappings().all()
        return business_system_list

    @classmethod
    @with_session(session_key='secondary')
    async def get_resource_list(cls, session: AsyncSession):
        resource_list = (
            await session.execute(
                select(
                    ResourceInfo.id, 
                    ResourceInfo.resource_name,
                    ResourceInfo.resource_type,
                    ResourceInfo.database_schema,
                    ResourceInfo.system_name,
                    ResourceInfo.data_owner,
                    ResourceInfo.data_manager,
                    ResourceInfo.create_time,
                    ResourceInfo.update_time
                )
            )
        ).mappings().all()
        return resource_list

    @classmethod
    @with_session(session_key='secondary')
    async def count_data_resource(cls, session: AsyncSession):
        id_object = (
            await session.execute(
                select(
                    func.min(DataResource.id).label("min_id"),
                    func.max(DataResource.id).label("max_id")
                )
                .select_from(DataResource)
            )
        ).mappings().one()
        return id_object

    @classmethod
    @with_session(session_key='secondary')
    async def get_data_resource_list(cls, session: AsyncSession, start_id: int, end_id: int):
        data_resource_list = (
            await session.execute(
                select(
                    DataResource.id,
                    DataResource.task_type,
                    DataResource.resource_id,
                    DataResource.resource_name,
                    DataResource.system_name,
                    DataResource.database_schema,
                    DataResource.database_table,
                    DataResource.file_name,
                    DataResource.field_name,
                    DataResource.data_owner,
                    DataResource.data_manager,
                    DataResource.create_time,
                    DataResource.update_time
                )
                .where(
                    DataResource.id >= start_id,
                    DataResource.id < end_id
                )
            )
        ).mappings().all()
        return data_resource_list

    @classmethod
    @with_session(session_key='secondary')
    async def count_data_directory(cls, session: AsyncSession):
        id_object = (
            await session.execute(
                select(
                    func.min(DataDirectory.id).label("min_id"),
                    func.max(DataDirectory.id).label("max_id")
                )
                .select_from(DataDirectory)
                .where(DataDirectory.main_data_type == 1)
            )
        ).mappings().one()
        return id_object

    @classmethod
    @with_session(session_key='secondary')
    async def get_data_directory_list(cls, session: AsyncSession, start_id: int, end_id: int):
        data_directory_list = (
            await session.execute(
                select(
                    DataDirectory.task_type,
                    DataDirectory.resource_id,
                    DataDirectory.resource_name,
                    DataDirectory.system_name,
                    DataDirectory.scan_id,
                    DataDirectory.data_owner,
                    DataDirectory.data_manager,
                    DataDirectory.file_name,
                    DataDirectory.field_name,
                    DataDirectory.data_category_1,
                    DataDirectory.data_category_2,
                    DataDirectory.data_category_3,
                    DataDirectory.data_category_4,
                    DataDirectory.data_level,
                    DataDirectory.create_time,
                    DataDirectory.update_time
                )
                .where(
                    DataDirectory.id >= start_id,
                    DataDirectory.id < end_id,
                    DataDirectory.main_data_type == 1
                )
            )
        ).mappings().all()
        return data_directory_list
